package com.stone.message.adapter.infra.publisher;

import com.stone.message.adapter.domain.event.MessageEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.lang.NonNull;

/**
 * 消息发送器
 *
 * @author Mr_wenpan@163.com 2021/6/28 11:34 上午
 */
public class MessagePublisher implements ApplicationEventPublisherAware {
    /**
     * 事件发布器
     */
    private ApplicationEventPublisher publisher;

    /**
     * 设置spring应用事件发布器
     *
     * @param applicationEventPublisher 当容器启动时就会调用所有aware的实现类，然后将applicationEventPublisher赋值到publisher上
     * @author Mr_wenpan@163.com 2021/6/28 11:35 上午
     */
    @Override
    public void setApplicationEventPublisher(@NonNull ApplicationEventPublisher applicationEventPublisher) {
        publisher = applicationEventPublisher;
    }

    /**
     * 生产消息
     *
     * @param messageEvent 消息事件
     */
    public void publish(MessageEvent messageEvent) {
        publisher.publishEvent(messageEvent);
    }

}